import GameData from "./GameData";

/**
 * @Author: 陈瑞鹏
 * @Date:   2022-11-01 12:01:52
 * @Last Modified by:   陈瑞鹏
 * @Last Modified time: 2024-12-28 16:17:28
 */
const { ccclass, property } = cc._decorator;
@ccclass
export default class ItemEle extends cc.Component {

  @property({ type: cc.Label, tooltip: "排序" })
  rankTxt: cc.Label = null;

  @property({ type: cc.Sprite, tooltip: "头像" })
  headSp: cc.Sprite = null;

  @property({ type: cc.Label, tooltip: "昵称" })
  nickName: cc.Label = null;

  @property({ type: cc.Label, tooltip: "获得的钱数" })
  moneyTxt: cc.Label = null;

  @property({ type: cc.Node, tooltip: "准备节点" })
  readyNode: cc.Node = null;

  @property({ type: cc.Label, tooltip: "准备文本" })
  readyTxt: cc.Label = null;

  @property({ type: cc.Label, tooltip: "是否房主" })
  isMaster: cc.Label = null;

  @property({ type: cc.Node, tooltip: "" })
  bgNode: cc.Node = null;

  protected onEnable(): void {

  }

  public drawUi(data) {
    // return;
    this.rankTxt.string = `${data.rank}`;
    if (this.bgNode)
      this.bgNode.active = (data.rank % 2 == 0);
    let that = this;
    that.headSp.spriteFrame = null;

    let len = this.nickName.node.width > 150 ? 8 : 5;
    this.nickName.string = `${data.nickName.substr(0, len)}`;

    this.moneyTxt.string = `${data.score || 0}`;
    let color = (data.isOwner) ? new cc.Color().fromHEX("#00FF19") : new cc.Color().fromHEX("#33A5FE");
    this.nickName.node.color = color;
    if (data.userId == GameData.ins.gameState.ownerId) {
      this.isMaster.string = "(房主)";
      this.isMaster.fontSize = 30;
      this.isMaster.node.color = new cc.Color().fromHEX("##44FF57")
      this.nickName.node.color = new cc.Color().fromHEX("#FF0000")
    } else if (data.isLeft) {
      this.isMaster.string = "(已离开)";
      this.isMaster.node.color = new cc.Color().fromHEX("#808080")
      this.nickName.node.color = new cc.Color().fromHEX("#808080")
    } else {
      this.isMaster.string = "";
    }
    cc.assetManager.loadRemote(`${data.avatar}`, { ext: '.png' }, (err, texture: any) => {
      // console.log(texture)
      if (err) {

        return;
      }
      if (!that.headSp) return;
      let sp = new cc.SpriteFrame(texture);
      that.headSp.spriteFrame = sp;
    })
  }
}
